package com.controller;

import com.service.SocketService;
import com.entity.Boy;
import com.repository.BoyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/boy")
public class BoyController  {
    @Autowired
    private BoyRepository boyRepository;
    @Autowired
    private RestTemplate restTemplate;
    private String url = "http://127.0.0.1:9060/girl/add/";

    @GetMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public String add(){
        try {
            String result = restTemplate.getForObject(url, String.class);
            System.out.println(result);
            String name = (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1))) + "";
            Boy boy = new Boy();
            boy.setName(name);
            boyRepository.save(boy);
            //调用另一个服务的接口

            if(1==1){
                throw new RuntimeException("error:1");
            }
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
